package top.ranzhang.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.springframework.beans.BeanUtils;
import top.ranzhang.bean.dto.CoashDTO;
import top.ranzhang.bean.entity.Child;
import top.ranzhang.bean.entity.Coash;
import top.ranzhang.bean.vo.ChildNameIdVO;
import top.ranzhang.bean.vo.CoashClazzCountVO;
import top.ranzhang.bean.vo.CoashUsernameIdVO;
import top.ranzhang.mapper.CoashMapper;
import top.ranzhang.service.ICoashService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ran
 * @since 2025-04-14
 */
@Service
public class CoashServiceImpl extends ServiceImpl<CoashMapper, Coash> implements ICoashService {

    @Override
    public void add(CoashDTO coashDTO) {
        Coash coash = new Coash();
        BeanUtils.copyProperties(coashDTO,coash);
        LocalDateTime now = LocalDateTime.now();
        coash.setCreateTime(now);
        coash.setUpdateTime(now);
        save(coash);
    }


    @Override
    public List<CoashUsernameIdVO> getAllUsername() {
        // 查询所有老师姓名和id
        List<Coash> list = Db.lambdaQuery(Coash.class)
                .select(Coash::getId, Coash::getUsername).list();
        // 封装成vo
        List<CoashUsernameIdVO> coashUsernameIdVOS = new ArrayList<>();
        for(Coash coash: list){
            CoashUsernameIdVO coashUsernameIdVO = new CoashUsernameIdVO();
            BeanUtils.copyProperties(coash,coashUsernameIdVO);
            coashUsernameIdVOS.add(coashUsernameIdVO);
        }
        return coashUsernameIdVOS;
    }

    /**
     * 获取所有老师的上课次数
     * @return
     */
    @Override
    public CoashClazzCountVO getCoashClazzCount() {
        // 查询所有老师姓名和id
        List<Coash> list = Db.lambdaQuery(Coash.class)
                .select(Coash::getUsername, Coash::getClazzNumber)
                .list();
        List<String> usernames = new ArrayList<>();
        List<Integer> clazzNumbers = new ArrayList<>();
        // 封装成vo
        list.stream().forEach(coash -> {
            usernames.add(coash.getUsername());
            clazzNumbers.add(coash.getClazzNumber());
        });
        return CoashClazzCountVO.builder()
                .usernameList(usernames)
                .countList(clazzNumbers)
                .build();
    }
}
